import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.border.EmptyBorder;
import javax.swing.text.AbstractDocument.BranchElement;
import javax.swing.text.AttributeSet;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;

public class Editor extends JFrame {

	private JPanel contentPane;
	private JTextPane txtPane;
	private JTextPane textPane;

	
	//	Get starting and ending position for bold and italic fonts
	
	private ArrayList<Integer> startingPositionPlain = new ArrayList<Integer>();
	private ArrayList<Integer> endingPositionPlain = new ArrayList<Integer>();
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Editor frame = new Editor();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Editor() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 531, 344);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JButton btnNewButton = new JButton("Bold");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				// format.setFormat(txtPane, BOLD);

				StyledDocument doc = txtPane.getStyledDocument();

				// Set Italic false
				Style styleItalic = txtPane.addStyle("Italic", null);
				StyleConstants.setItalic(styleItalic, false);

				doc.setCharacterAttributes(
						txtPane.getSelectionStart(),
						txtPane.getSelectionEnd() - txtPane.getSelectionStart(),
						txtPane.getStyle("Italic"), true);

				// Set Bold true

				Style styleBold = txtPane.addStyle("Bold", null);
				StyleConstants.setBold(styleBold, true);

				doc.setCharacterAttributes(
						txtPane.getSelectionStart(),
						txtPane.getSelectionEnd() - txtPane.getSelectionStart(),
						txtPane.getStyle("Bold"), false);
				
				
				//	Store starting position
				
				startingPositionPlain.add(txtPane.getSelectionStart());
				
				//	Store ending position
				
				endingPositionPlain.add(txtPane.getSelectionEnd());
			}
		});
		btnNewButton.setBounds(10, 11, 89, 23);
		contentPane.add(btnNewButton);

		JButton btnItalic = new JButton(" Italic");
		btnItalic.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				// format.setFormat(txtPane, ITALIC);

				// Set Bold false

				StyledDocument doc = txtPane.getStyledDocument();
				Style styleBold = txtPane.addStyle("Bold", null);
				StyleConstants.setItalic(styleBold, false);

				doc.setCharacterAttributes(
						txtPane.getSelectionStart(),
						txtPane.getSelectionEnd() - txtPane.getSelectionStart(),
						txtPane.getStyle("Bold"), true);

				// Set Italic true

				Style styleItalic = txtPane.addStyle("Italic", null);
				StyleConstants.setItalic(styleItalic, true);

				doc.setCharacterAttributes(
						txtPane.getSelectionStart(),
						txtPane.getSelectionEnd() - txtPane.getSelectionStart(),
						txtPane.getStyle("Italic"), false);
				

				//	Store starting position
				
				startingPositionPlain.add(txtPane.getSelectionStart());
				
				//	Store ending position
				
				endingPositionPlain.add(txtPane.getSelectionEnd());
				
			}
		});
		btnItalic.setBounds(109, 11, 89, 23);
		contentPane.add(btnItalic);

		txtPane = new JTextPane();
		txtPane.setContentType("text/html");
		txtPane.setText("Sunil Agrawal was the student of Anand mechantile college of science, management and technology.");
		txtPane.setBounds(20, 45, 374, 44);
		contentPane.add(txtPane);

		JButton btnBoldItalic = new JButton("Bold / Italic");
		btnBoldItalic.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				// format.setFormat(txtPane, BOLDITALIC);

				// Set Italic true
				StyledDocument doc = txtPane.getStyledDocument();
				Style styleItalic = txtPane.addStyle("Italic", null);
				StyleConstants.setItalic(styleItalic, true);

				doc.setCharacterAttributes(
						txtPane.getSelectionStart(),
						txtPane.getSelectionEnd() - txtPane.getSelectionStart(),
						txtPane.getStyle("Italic"), false);

				// Set Bold true
				Style styleBold = txtPane.addStyle("Bold", null);
				StyleConstants.setBold(styleBold, true);

				doc.setCharacterAttributes(
						txtPane.getSelectionStart(),
						txtPane.getSelectionEnd() - txtPane.getSelectionStart(),
						txtPane.getStyle("Bold"), false);
				
				//	Store starting position
				
				startingPositionPlain.add(txtPane.getSelectionStart());
				
				//	Store ending position
				
				endingPositionPlain.add(txtPane.getSelectionEnd());

			}
		});
		btnBoldItalic.setBounds(208, 11, 89, 23);
		contentPane.add(btnBoldItalic);

		JButton btnNormal = new JButton("Normal");
		btnNormal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent ae) {
				// format.setFormat(txtPane, NORMAL);

				// Set Italic false
				StyledDocument doc = txtPane.getStyledDocument();
				Style styleItalic = txtPane.addStyle("Italic", null);
				StyleConstants.setItalic(styleItalic, false);

				doc.setCharacterAttributes(
						txtPane.getSelectionStart(),
						txtPane.getSelectionEnd() - txtPane.getSelectionStart(),
						txtPane.getStyle("Italic"), true);

				// Set Bold false
				Style styleBold = txtPane.addStyle("Bold", null);
				StyleConstants.setBold(styleBold, false);

				doc.setCharacterAttributes(
						txtPane.getSelectionStart(),
						txtPane.getSelectionEnd() - txtPane.getSelectionStart(),
						txtPane.getStyle("Bold"), true);
				
				//	Store starting position
				
				startingPositionPlain.add(txtPane.getSelectionStart());
				
				//	Store ending position
				
				endingPositionPlain.add(txtPane.getSelectionEnd());

			}
		});
		btnNormal.setBounds(305, 11, 89, 23);
		contentPane.add(btnNormal);

		JButton btnInsert = new JButton("Insert");
		btnInsert.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {

					  String strHTML = txtPane.getText();

						//	Sort starting position and ending position
						
						Document docPlain = txtPane.getDocument(); 
						String strPlain = docPlain.getText(0, docPlain.getLength()); 

						Collections.sort(startingPositionPlain);
						Collections.sort(endingPositionPlain);
						
						for(int i=0; i<startingPositionPlain.size();i++){
							System.out.println(startingPositionPlain.get(i) + "  " + endingPositionPlain.get(i) + "\n\n");
						}
						
						  //	Login to get plain text starts here 

					 int j = 0;
					 for(int i=0; i<strHTML.length(); i++){
						 if(i+6 <= strHTML.length() && strHTML.substring(i, i+6).contains("<Bold>")){
							 System.out.println("j..." + j);
							 String oldString = strHTML.substring(i, i+6);	//	Get value Bold
							 String newString = strPlain.substring(startingPositionPlain.get(j), endingPositionPlain.get(j));	//	Get String from plain text
							 strHTML = strHTML.replaceFirst(oldString, newString);	//	Replace old string with new string
							 ++j;
						 }
						 
						 if(i+8 <= strHTML.length() && strHTML.substring(i, i+8).contains("<Italic>")){
							 System.out.println("j..." + j);
							 String oldString = strHTML.substring(i, i+8);	//	Get value Bold
							 String newString = strPlain.substring(startingPositionPlain.get(j), endingPositionPlain.get(j));	//	Get String from plain text
							 strHTML = strHTML.replaceFirst(oldString, newString);	//	Replace old string with new string
							 ++j;
						 }
					 }

					 
					 //	Insert record into database
					 
					 DescriptionModel descriptionModel = new DescriptionModel();
					 descriptionModel.insert(strHTML);
					  

				} catch (Exception exception) {
					exception.printStackTrace();
				}

			}
		});
		btnInsert.setBounds(28, 272, 97, 23);
		contentPane.add(btnInsert);

		textPane = new JTextPane();
		textPane.setContentType("text/html");
		textPane.setBounds(30, 100, 374, 91);
		contentPane.add(textPane);

		JButton btnGet = new JButton("Get");
		btnGet.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					DescriptionModel descriptionModel = new DescriptionModel();
					ResultSet rs = descriptionModel.get();
					rs.last();
					textPane.setText(rs.getString("description"));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		btnGet.setBounds(135, 272, 97, 23);
		contentPane.add(btnGet);
	}

}

/*class MyHtml2Text extends HTMLEditorKit.ParserCallback {
    StringBuffer s;
    public MyHtml2Text() {}
    public void parse(Reader in) throws IOException {
        s = new StringBuffer();
        ParserDelegator delegator = new ParserDelegator();
        delegator.parse(in, this, Boolean.TRUE);
    }
    public void handleText(char[] text, int pos) {
        s.append(text);
        s.append("\n");
    }
    public String getText() {
        return s.toString();
    }
}*/